import { Body, JsonController, BodyParam, Post, Put, Get, QueryParam, Delete, Param } from 'routing-controllers';
import { Inject } from "typedi";
import { DeptService } from '../services/DeptService';
import { Dept} from "../entitys/Dept"
import Response from '../common/response';
@JsonController("/dept")
export default class DeptController{
    @Inject()
    deptService: DeptService
    @Post()
    async addDept(@BodyParam("name", {required:true})name:string,@BodyParam("parentId")parentId:number,@BodyParam("sort")sort:number) {
        let dept = new Dept();
        dept.name = name
        dept.parentId = parentId
        dept.sort = sort
        try {
            await this.deptService.addDept(dept)    
        } catch (error) {
            return Response.BAD_REQUEST(error.message)
        }
        return Response.SUCCESS()
    }
    @Put()
    async updateDept(@BodyParam("id", {required:true})id:number,@BodyParam("name", {required:true})name:string,@BodyParam("parentId")parentId:number,@BodyParam("sort")sort:number) {
        let dept = new Dept()
        dept.name = name;
        dept.parentId = parentId
        dept.sort = sort
        dept.id = id
        try {
            await this.deptService.updateDept(dept);
        } catch (error) {
            return Response.BAD_REQUEST(error.message)
        }
        return Response.SUCCESS()
    }
    @Get("/list")
    async getDeptTree(@QueryParam("deptName")deptName:string,@QueryParam("dataStatus")dataStatus:number) {
        const depts = await this.deptService.getDeptTree(deptName, dataStatus)
        return Response.SUCCESS(depts)
    }
    @Delete("/delById/:id")
    async delDept(@Param("id") id: number) {
        try {
            await this.deptService.delDept(id)     
        } catch (error) {
            return Response.BAD_REQUEST(error.message)
        }
        return Response.SUCCESS()
    }
    @Get("/getDeptById/:id")
    async getDeptById(@Param("id")id:number) {
        const dept = await this.deptService.getDeptById(id);
        return Response.SUCCESS(dept)
    }
}